<template>
  <div class='container'>
     <!-- 表单内容 -->
    <el-form
    ref="form"
    :model="form"
    :rules="rules"
    label-width="100px"
    >
      <el-form-item
      label="学科名称"
      prop="subjectName"
      >
        <el-input v-model="form.subjectName"></el-input>
      </el-form-item>
       <el-form-item label="是否显示">
         <el-switch
          active-color="#13ce66"
          inactive-color="#ff4949"
          v-model="form.isFrontDisplay"
          :active-value="1"
          :inactive-value="0"
         />
       </el-form-item>
    </el-form>
        <template >
          <div style="text-align: right;">
            <el-button @click="$emit('close')">取消</el-button>
            <el-button type="primary" @click="hsubmit">确定</el-button>
          </div>
        </template>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects.js'
export default {
  props: {
    curid: {
      type: Number,
      required: true
    },
    list: {
      type: Array,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    row: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      // 手机表单的数据对象
      form: {
        subjectName: '',
        isFrontDisplay: 1 // 启用
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getdetail()
  },
  methods: {
    // 清空表单
    resetform () {
      this.$refs.form.resetFields()
      this.form = {
        subjectName: '',
        isFrontDisplay: 0 // 启用
      }
    },
    // 获取学科详细信息
    async getdetail () {
      this.form.subjectName = this.row.subjectName
      this.form.isFrontDisplay = this.row.isFrontDisplay
      debugger
    },
    // 编辑
    async doedit () {
      try {
        const data = { id: this.curid, subjectName: this.form.subjectName }
        console.log(data)
        debugger
        const res = await update({ id: this.curid, subjectName: this.form.subjectName })
        console.log('update', res)
        this.$emit('success')

        // 保存数据
      } catch (err) {
        console.log('update', err)
      }
    },
    // 新增学科开弹窗
    async doadd () {
      try {
        const res = await add(this.form)
        console.log('add', res)
        this.$emit('success')
        this.resetform()
        // 保存数据
      } catch (err) {
        console.log('add', err)
      }
    },

    // 点击确定按钮提交
    hsubmit () {
      // 兜底校验
      this.$refs.form.validate((valid) => {
        if (!valid) return // 校验失败

        this.isEdit ? this.doedit() : this.doadd()

        this.form = {
          subjectName: '',
          isFrontDisplay: 1 // 启用
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
